package com.itic.mobile.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.code.persistence.model.CodeDetail;
import com.itic.appbase.applications.sys.code.vo.CodeDetailVo;
import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.mobile.base.BaseMobileInterface;
import com.itic.system.xhbanner.persistence.model.Banner;
import com.itic.system.xhbanner.persistence.model.BannerExample;
import com.itic.system.xhbanner.service.BannerService;
import com.itic.system.xhcategory.persistence.model.XhCategory;
import com.itic.system.xhcategory.persistence.model.XhCategoryExample;
import com.itic.system.xhcategory.service.XhCategoryService;
import com.itic.system.xhproduct.persistence.model.XhProduct;
import com.itic.system.xhproduct.persistence.model.XhProductExample;
import com.itic.system.xhproduct.persistence.model.XhProductExample.Criteria;
import com.itic.system.xhproduct.service.XhProductService;
import com.itic.system.xhuser.persistence.model.XhUser;
import com.itic.system.xhuserstore.persistence.model.XhUserStore;
import com.itic.system.xhuserstore.persistence.model.XhUserStoreExample;
import com.itic.system.xhuserstore.service.XhUserStoreService;


/**
 * 现货登入接口类
 * @author 许小宝
 * 2017年07月25日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/product")
public class ProductInterface
     extends BaseMobileInterface {
	@Autowired
    private XhCategoryService xhCategoryService;
    @Autowired
    private XhProductService xhProductService;
    @Autowired
    private XhUserStoreService xhUserStoreService;
    @Autowired
    private OutCommonService outCommonService;
    @Autowired
    private BannerService bannerService;
    
   // 返回属性黑名单
    private static String[] ignoreProperties = new String[] { "createUser", "isEffective", "createTime", "updateTime",
            "orgCode" };

    /**
     * 现货首页
     * @return
     */
    @RequestMapping(value="/index")
    @ResponseBody
    public Map<String,Object> send( HttpServletRequest request,HttpServletResponse response ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		
    		Map<String, Object> dataMap = new HashMap<String,Object>();
    		dataMap.put("banners", getLunbo());
    	    dataMap.put("products", ValueHelper.tranList2MapList(getAdviceProducts(), ignoreProperties));
    	    dataMap.put("categorys", ValueHelper.tranList2MapList(getCategorys(), ignoreProperties));
    	    dataMap.put("news", ValueHelper.tranList2MapList(getNewProducts(), ignoreProperties));
    	    resultMap.put(DATA, dataMap);
    	    errorCode = SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("ProductInterface", "index", e.toString(),"现货首页",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
    /**
     * 现货首页
     * @return
     */
    @RequestMapping(value="/propertys")
    @ResponseBody
    public Map<String,Object> propertys( HttpServletRequest request,HttpServletResponse response ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		
    		ArrayList<PropertyVo> datas = new ArrayList<PropertyVo>();
    		
    		PropertyVo shapes = new PropertyVo();
    		shapes.setName("形状");
    		CodeDetailVo vo = new CodeDetailVo();
    		vo.setType("T_XH_PRODUCT_SHAPE");
    		List<CodeDetail> shapeCodes = outCommonService.getCodeDetail(vo);
    		shapes.setCodes(ValueHelper.tranList2MapList(shapeCodes, ignoreProperties));
    		datas.add(shapes);
    		
    		vo.setType("T_XH_PRODUCT_AREA");
    		PropertyVo areas = new PropertyVo();
    		areas.setName("种地");
    		List<CodeDetail> areaCodes = outCommonService.getCodeDetail(vo);
    		areas.setCodes(ValueHelper.tranList2MapList(areaCodes, ignoreProperties));
    		datas.add(areas);
    		
    		vo.setType("T_XH_PRODUCT_KOUJING");
    		PropertyVo koujings = new PropertyVo();
    		koujings.setName("口径");
    		List<CodeDetail> koujingCodes = outCommonService.getCodeDetail(vo);
    		koujings.setCodes(ValueHelper.tranList2MapList(koujingCodes, ignoreProperties));
    		datas.add(koujings);
    		
    	    resultMap.put(DATA, datas);
    	    
    	    errorCode = SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("ProductInterface", "index", e.toString(),"现货首页",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
    
    
    /**
     * 现货首页
     * @return
     */
    @RequestMapping(value="/filter")
    @ResponseBody
    public Map<String,Object> filter( HttpServletRequest request,HttpServletResponse response,String koujingId,String areaId,String shapeId,String token ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		Map<String, Object> dataMap = new HashMap<String,Object>();
    		
    		XhProductExample example = new XhProductExample();
    		example.setOrderByClause("CREATE_TIME DESC");
    		example.setOrderByClause("REORDER DESC");
    		Criteria criteria = example.createCriteria();
    		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    		if( StringHelper.isNotBlank(koujingId)){
    			criteria.andKoujingEqualTo(koujingId);
    		}
    		if( StringHelper.isNotBlank(areaId)){
    			criteria.andAreaEqualTo(areaId);
    		}
    		if( StringHelper.isNotBlank(shapeId)){
    			criteria.andShapeEqualTo(shapeId);
    		}
    		this.handlePageQuery(request, example);
    		
    		int count = xhProductService.count(example);
    		List<XhProduct> list = xhProductService.list(example);
    		list = setStore(token,list);
    		String isMore = isMore(request,count);
    		
    		dataMap.put("list", ValueHelper.tranList2MapList(list, ignoreProperties));
    		dataMap.put("count", count);
    		dataMap.put("isMore", isMore);
    		
    	    resultMap.put(DATA, dataMap);
    	    errorCode = SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("ProductInterface", "index", e.toString(),"现货首页",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    public List<String> getSubCategoryIds(String categoryId){
    	List<String> result = new ArrayList<String>();
    	result.add(categoryId);
    	XhCategoryExample example = new XhCategoryExample();
    	example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andParentIdEqualTo(categoryId);
    	List<XhCategory> list = xhCategoryService.list(example);
    	if( list.size()>0 ){
    		for( XhCategory temp :list ){
    			result.add(temp.getId());
    		}
    	}
    	return result;
    }
    
    /**
     * 现货首页
     * @return
     */
    @RequestMapping(value="/query")
    @ResponseBody
    public Map<String,Object> query( HttpServletRequest request,HttpServletResponse response,String categoryId, String name, String token ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		Map<String, Object> dataMap = new HashMap<String,Object>();
    		
    		XhProductExample example = new XhProductExample();
    		example.setOrderByClause("CREATE_TIME DESC");
    		example.setOrderByClause("REORDER DESC");
    		Criteria criteria = example.createCriteria();
    		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    		if( StringHelper.isNotBlank(categoryId) && !"0".equals(categoryId) ){
    			List<String> subCategoryIds = getSubCategoryIds(categoryId);
    			//criteria.andCategoryIdEqualTo(categoryId);
    			criteria.andCategoryIdIn(subCategoryIds);
    		}
    		if( StringHelper.isNotBlank(name) ){
    			criteria.andNameLike("%"+name+"%");
    		}
    		this.handlePageQuery(request, example);
    		
    		int count = xhProductService.count(example);
    		List<XhProduct> list = xhProductService.list(example);
    		list = setStore(token,list);
    		String isMore = isMore(request,count);
    		
    		dataMap.put("list", ValueHelper.tranList2MapList(list, ignoreProperties));
    		dataMap.put("count", count);
    		dataMap.put("isMore", isMore);
    		
    	    resultMap.put(DATA, dataMap);
    	    errorCode = SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("ProductInterface", "index", e.toString(),"现货首页",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
    /**
     * 获取所有类别，有父子关系
     * @return
     */
    @RequestMapping(value="/categorys")
    @ResponseBody
    public Map<String, Object> categorys( HttpServletRequest request,HttpServletResponse response ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		resultMap.put(DATA,  ValueHelper.tranList2MapList(getCategorys(), ignoreProperties));
    	    errorCode = SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("ProductInterface", "categorys", e.toString(),"获得所有类别",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    /**
     * 查看商品详情
     * @return
     */
    @RequestMapping(value="/detail")
    @ResponseBody
    public Map<String,Object> reg( HttpServletRequest request, HttpServletResponse response, String productId, String token ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(productId) ){
    			Map<String, Object> dataMap = new HashMap<String,Object>();
    			XhProduct xhProduct = xhProductService.get(productId);
    			if( StringHelper.isEmpty(xhProduct.getVideoImage()) ){
    				xhProduct.setVideoImage( getDefaultVideoImage() );
    			}
    			xhProduct = setStore(token, xhProduct);
    			
    			dataMap.put("detail", xhProduct);
    			dataMap.put("suggests", ValueHelper.tranList2MapList(getSuggestById(productId,token), ignoreProperties));
    			resultMap.put(DATA, dataMap);
    			errorCode = SUCCESS_ERROR_CODE;
    	        errorMsg = SUCCESS_ERROR_MSG;
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	logException("LoginInterface", "reg", e.toString(),"用户注册",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }  
    
    /**
     * 获取所有类别，有父子关系
     * @return
     */
    @RequestMapping(value="/categorySub")
    @ResponseBody
    public Map<String, Object> categorySub( HttpServletRequest request,HttpServletResponse response ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		ArrayList<CategoryVo> list = new ArrayList<CategoryVo>();
    		List<XhCategory> categorys = getCategorys();
    		if( categorys.size() > 0 ){
    			for( XhCategory tmp : categorys ){
    				CategoryVo categoryVo = new CategoryVo();
    				categoryVo.setId(tmp.getId());
    				categoryVo.setName(tmp.getName());
    				categoryVo.setParentId(tmp.getParentId());
    				categoryVo.setSubCategorys(getSubCategorys(tmp.getId()));
    				list.add(categoryVo);
    			}
    		}
    		resultMap.put(DATA,  ValueHelper.tranList2MapList(list, ignoreProperties));
    	    errorCode = SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("ProductInterface", "categorys", e.toString(),"获得所有类别",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    public List<XhCategory> getSubCategorys( String parentId ){
    	List<XhCategory> result = null;
    	if( StringHelper.isNotBlank(parentId) ){
    		XhCategoryExample example = new XhCategoryExample();
    		example.setOrderByClause("CREATE_TIME DESC");
        	example.setOrderByClause("REORDER DESC");
    		example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andParentIdEqualTo(parentId);
    		result = xhCategoryService.list(example);
    	}
    	return result;
    }
    
    
    /**
     * 这个方法研究
     * @param productId
     * @return
     */
    public List<XhProduct> getSuggestById( String productId, String token ){
    	return getProducts(token);
    }
   

    
    /**
     * 添加收藏
     * @return
     */
    @RequestMapping(value="/toggleStore")
    @ResponseBody
    public Map<String,Object> toggleStore( HttpServletRequest request,HttpServletResponse response, String token, String productId ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(productId) ){
    			XhUser user = getUserByToken(token);
    			if( null != user ){
    				XhProduct xhProduct = xhProductService.get(productId);
    				if( null != xhProduct ){
    					//判断 是否已收藏了
    					XhUserStoreExample example = new XhUserStoreExample();
    					example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andUserIdEqualTo(user.getId()).andProductIdEqualTo(productId);
    					List<XhUserStore> list = xhUserStoreService.list(example);
    					if( list.size() > 0 ){
    						XhUserStore store = list.get(0);
    						xhUserStoreService.delete(store);
    						resultMap.put(DATA, "002");
    						errorCode = SUCCESS_ERROR_CODE;
            	        	errorMsg = SUCCESS_ERROR_MSG;
    					}else{
    						XhUserStore store = new XhUserStore();
        					store.setPrice(xhProduct.getPrice());
        					store.setProductId(productId);
        					store.setUserId(user.getId());
        					store.setName(xhProduct.getName());
        					String[] split = xhProduct.getImages().split(",");
        					if( split.length > 0 ){
        						store.setImage( split[0] );
        					}
        					xhUserStoreService.add(store);
        					resultMap.put(DATA, "001");
        				    errorCode = SUCCESS_ERROR_CODE;
        			        errorMsg = SUCCESS_ERROR_MSG;
    					}
    				}else{
    					errorCode = PRODUCT_ID_ERROR_CODE;
        	        	errorMsg = PRODUCT_ID_ERROR_MSG;
    				}
    			}else{
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("ProductInterface", "toStore", e.toString(),"添加收藏",e);
		
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
    
    /**
     * 添加收藏
     * @return
     */
    @RequestMapping(value="/addStore")
    @ResponseBody
    public Map<String,Object> addStore( HttpServletRequest request,HttpServletResponse response, String token, String productId ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(productId) ){
    			XhUser user = getUserByToken(token);
    			if( null != user ){
    				XhProduct xhProduct = xhProductService.get(productId);
    				if( null != xhProduct ){
    					//判断 是否已收藏了
    					XhUserStoreExample example = new XhUserStoreExample();
    					example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andUserIdEqualTo(user.getId()).andProductIdEqualTo(productId);
    					List<XhUserStore> list = xhUserStoreService.list(example);
    					if( list.size() > 0 ){
    						errorCode = PRODUCT_STORED_ERROR_CODE;
            	        	errorMsg = PRODUCT_STORED_ERROR_MSG;
    					}else{
    						XhUserStore store = new XhUserStore();
        					store.setPrice(xhProduct.getPrice());
        					store.setProductId(productId);
        					store.setUserId(user.getId());
        					store.setName(xhProduct.getName());
        					String[] split = xhProduct.getImages().split(",");
        					if( split.length > 0 ){
        						store.setImage( split[0] );
        					}
        					xhUserStoreService.add(store);
        				    errorCode = SUCCESS_ERROR_CODE;
        			        errorMsg = SUCCESS_ERROR_MSG;
    					}
    				}else{
    					errorCode = PRODUCT_ID_ERROR_CODE;
        	        	errorMsg = PRODUCT_ID_ERROR_MSG;
    				}
    			}else{
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("ProductInterface", "toStore", e.toString(),"添加收藏",e);
		
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    /**
     * 取消收藏
     * @return
     */
    @RequestMapping(value="/cancelStore")
    @ResponseBody
    public Map<String,Object> cancelStore( HttpServletRequest request,HttpServletResponse response, String token, String productId ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(productId) ){
    			XhUser user = getUserByToken(token);
    			if( null != user ){
    				XhUserStoreExample example = new XhUserStoreExample();
    				example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andUserIdEqualTo(user.getId()).andProductIdEqualTo(productId);
    				List<XhUserStore> list = xhUserStoreService.list(example);
    				if( list.size() > 0 ){
    					XhUserStore store = new XhUserStore();
    					xhUserStoreService.deleteByExample(store, example);
    					errorCode = SUCCESS_ERROR_CODE;
      			        errorMsg = SUCCESS_ERROR_MSG;
    				}else{
    					errorCode = PRODUCT_ID_ERROR_CODE;
        	        	errorMsg = PRODUCT_ID_ERROR_MSG;
    				}
    			}else{
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("ProductInterface", "cannelStore", e.toString(),"取消收藏",e);
		
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    /**
     * 查看更多新品
     * @return
     */
    @RequestMapping(value="/newMore")
    @ResponseBody
    public Map<String,Object> newMore( HttpServletRequest request,HttpServletResponse response, String token ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		Map<String, Object> dataMap = new HashMap<String,Object>();
			XhProductExample example = new XhProductExample();
	    	this.handlePageQuery( request, example );
			Criteria criteria = example.createCriteria();
			example.setOrderByClause("CREATE_TIME DESC");
			//example.setOrderByClause("REORDER ASC");
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
			criteria.andIsNewEqualTo(DBConst.TRUE);
			int count = xhProductService.count(example);
		    List<XhProduct> list = xhProductService.list(example);
		    String isMore = isMore(request,count);
			dataMap.put("count", count);
			dataMap.put("isMore", isMore);
			dataMap.put("list", ValueHelper.tranList2MapList(list, ignoreProperties));
			resultMap.put(DATA, dataMap);
			errorCode = SUCCESS_ERROR_CODE;
		    errorMsg = SUCCESS_ERROR_MSG;
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("ProductInterface", "cannelStore", e.toString(),"取消收藏",e);
		
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
    /**
     * 查看更多商品
     * @return
     */
    @RequestMapping(value="/productMore")
    @ResponseBody
    public Map<String,Object> productMore( HttpServletRequest request,HttpServletResponse response, String token ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		Map<String, Object> dataMap = new HashMap<String,Object>();
			XhProductExample example = new XhProductExample();
	    	this.handlePageQuery( request, example );
			Criteria criteria = example.createCriteria();
			example.setOrderByClause("CREATE_TIME DESC");
			example.setOrderByClause("REORDER DESC");
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
			criteria.andIsAdviceEqualTo(DBConst.TRUE);
			int count = xhProductService.count(example);
		    List<XhProduct> list = xhProductService.list(example);
		    String isMore = isMore(request,count);
		    
		    resultMap.put(DATA, dataMap);
			dataMap.put("count", count);
			dataMap.put("isMore", isMore);
			dataMap.put("list", ValueHelper.tranList2MapList(list, ignoreProperties));
			
			errorCode = SUCCESS_ERROR_CODE;
		        errorMsg = SUCCESS_ERROR_MSG;
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("ProductInterface", "cannelStore", e.toString(),"取消收藏",e);
		
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    public List<XhProduct> getProducts( ){
    	XhProductExample example = new XhProductExample();
    	this.handlePageQuery( example,"6" );
		Criteria criteria = example.createCriteria();
		example.setOrderByClause("CREATE_TIME DESC");
		example.setOrderByClause("REORDER DESC");
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
	    List<XhProduct> list = xhProductService.list(example);
	    return list;
    }
    
    public List<XhProduct> getProducts( String token ){
    	XhProductExample example = new XhProductExample();
    	this.handlePageQuery( example,"6" );
		Criteria criteria = example.createCriteria();
		example.setOrderByClause("CREATE_TIME DESC");
		example.setOrderByClause("REORDER DESC");
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
	    List<XhProduct> list = xhProductService.list(example);
	    list = setStore(token, list);
	    return list;
    }
    
    public List<XhProduct> getNewProducts( ){
    	XhProductExample example = new XhProductExample();
    	this.handlePageQuery( example,"4" );
		Criteria criteria = example.createCriteria();
		example.setOrderByClause("UPDATE_TIME DESC");
		//example.setOrderByClause("REORDER ASC");
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andIsNewEqualTo(DBConst.TRUE);
	    List<XhProduct> list = xhProductService.list(example);
	    return list;
    }
    
    public List<XhProduct> getAdviceProducts( ){
    	XhProductExample example = new XhProductExample();
    	this.handlePageQuery( example,"4" );
		Criteria criteria = example.createCriteria();
		example.setOrderByClause("UPDATE_TIME DESC");
		//example.setOrderByClause("REORDER ASC");
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andIsAdviceEqualTo(DBConst.TRUE);
	    List<XhProduct> list = xhProductService.list(example);
	    return list;
    }
    
    
   /* public List<BannerVo> getLunbo(){
    	ArrayList<BannerVo> result = new ArrayList<BannerVo>();
    	XhProductExample example = new XhProductExample();
		Criteria criteria = example.createCriteria();
		example.setOrderByClause("CREATE_TIME DESC");
		example.setOrderByClause("REORDER ASC");
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andIsLunboEqualTo("001");
	    List<XhProduct> list = xhProductService.list(example);
	    if( list.size() > 0 ){
	    	for( XhProduct tmp :list ){
	    		BannerVo vo = new BannerVo();
	    		vo.setId(tmp.getId());
	    		vo.setUrl(tmp.getImages().split(",")[0]);
	    		result.add(vo);
	    	}
	    }
	    return result;
    }*/
    
    public List<BannerVo> getLunbo(){
    	ArrayList<BannerVo> result = new ArrayList<BannerVo>();
    	BannerExample example = new BannerExample();
    	com.itic.system.xhbanner.persistence.model.BannerExample.Criteria criteria = example.createCriteria();
		example.setOrderByClause("CREATE_TIME DESC");
		example.setOrderByClause("REORDER ASC");
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		
	    List<Banner> list = bannerService.list(example);
	    if( list.size() > 0 ){
	    	for( Banner tmp :list ){
	    		BannerVo vo = new BannerVo();
	    		vo.setId(tmp.getType());
	    		vo.setUrl(tmp.getUrl());
	    		result.add(vo);
	    	}
	    }
	    return result;
    }
    
    public List<XhCategory> getCategorys(){
    	XhCategoryExample example = new XhCategoryExample();
    	example.setOrderByClause("CREATE_TIME DESC");
    	example.setOrderByClause("REORDER ASC");
    	example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andParentIdEqualTo("0");
    	List<XhCategory> list = xhCategoryService.list(example);
    	return list;
    }
    
    
    public XhProduct setStore( String token,XhProduct product ){
    	
		product.setIsStore("002");
    	if( StringHelper.isNotBlank(token) ){
			XhUser user = getUserByToken(token);
			if( null != user ){
				XhUserStoreExample example = new XhUserStoreExample();
				example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andUserIdEqualTo(user.getId()).andProductIdEqualTo(product.getId());
				List<XhUserStore> list = xhUserStoreService.list(example);
				if( list.size() > 0 ){
					product.setIsStore("001");
				}
			}
		}
    	
    	return product;
    }
    
    public List<XhProduct> setStore( String token,List<XhProduct> products ){
    
		if( products.size() > 0 ){
    		for( XhProduct tmp: products ){
    			tmp.setIsStore("002");
    	    	if( StringHelper.isNotBlank(token) ){
    				XhUser user = getUserByToken(token);
    				if( null != user ){
    					XhUserStoreExample example = new XhUserStoreExample();
        				example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andUserIdEqualTo(user.getId()).andProductIdEqualTo(tmp.getId());
        				List<XhUserStore> list = xhUserStoreService.list(example);
        				if( list.size() > 0 ){
        					tmp.setIsStore("001");
        				}
    				}
    			}
    		}
    	}
    	
    	return products;
    }
}